package com.desk.copyfile.main;

import com.desk.copyfile.utils.CopyFilesUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.List;

/**
 * @author zhangxi
 * @date 2024/03/15 22:40
 * @value jf 窗体对象
 * @value jl 展示标签
 */

@Component
public class MainWin extends JFrame {
    private MyConfigProperties myConfigProperties;
    private JSpinner startDateSpinner;
    private JSpinner endDateSpinner;
    private JTextArea resultArea;

    private JPanel checkboxPanel;
    private List<JCheckBox> checkboxes;
    private List<String> selectedPathNameList = new ArrayList<>();

    public MainWin() {
        // 现在可以安全地使用someProperties了
    }
    @Autowired
    public MainWin(MyConfigProperties someProperties) {
        this.myConfigProperties = someProperties;
        setTitle("日期选择器与多选列表示例");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(700, 500);
        setLayout(new BorderLayout(10, 10));
        // 创建日期Spinner
        creatStartEndDate();

        // 创建多选列表
        createCheckBoxList();

        // 创建查询按钮
        createSearchButton();

        // 结果展示区
        createResultArea();

        // 设置可见
        setVisible(true);
    }

    private void createResultArea() {
        resultArea = new JTextArea();
        resultArea.setEditable(false);
        JScrollPane resultScrollPane = new JScrollPane(resultArea);
        // 设置结果展示区的大小，使其不占据整个窗口的东半部分
        resultScrollPane.setPreferredSize(new Dimension(300, 100));
        add(resultScrollPane, BorderLayout.EAST);
    }

    private void createSearchButton() {
        JButton queryButton = new JButton("确认");
        queryButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 获取日期
                Date startDate = (Date) startDateSpinner.getValue();
                Date endDate = (Date) endDateSpinner.getValue();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                String startStr = sdf.format(startDate);
                String endStr = sdf.format(endDate);
                // 获取选中的列表项
                selectedPathNameList.clear();
                for (JCheckBox checkbox : checkboxes) {
                    if (checkbox.isSelected()) {
                        selectedPathNameList.add(checkbox.getText());
                    }
                }

                resultArea.setText("开始日期: " + startStr + "\n" +
                        "结束日期: " + endStr + "\n" +
                        "选中的列表项: " + selectedPathNameList.toString());

                // 执行具体逻辑
                CopyFilesUtils.copyFilesToOtherPath(startDate,endDate,selectedPathNameList, resultArea);
            }
        });
        add(queryButton, BorderLayout.SOUTH);
    }

    private void createCheckBoxList() {
        checkboxPanel = new JPanel();
        checkboxPanel.setLayout(new BoxLayout(checkboxPanel, BoxLayout.Y_AXIS));
        checkboxes = new ArrayList<>();
        JCheckBox selectAllCheckBox = new JCheckBox("全选");
        checkboxPanel.add(selectAllCheckBox);
        // 为全选复选框添加事件监听器
        selectAllCheckBox.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 根据全选复选框的选中状态来设置其他复选框的选中状态
                boolean isSelected = selectAllCheckBox.isSelected();
                for (JCheckBox checkbox : checkboxes) {
                    checkbox.setSelected(isSelected);
                }
            }
        });
        Map<String, MyConfigProperties.optionsSet> optionsMap = myConfigProperties.getOptionsMap();
        for (String keyName : optionsMap.keySet()) {
            MyConfigProperties.optionsSet optionsSet = optionsMap.get(keyName);
            JCheckBox checkbox = new JCheckBox(optionsSet.getName());
            checkbox.setSelected(optionsSet.isSelected());
            checkbox.setFont(checkbox.getFont().deriveFont(16f));
            checkbox.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    // Handle checkbox state change here if fneeded
                }
            });
            checkboxPanel.add(checkbox);
            checkboxes.add(checkbox);
        }

        JScrollPane scrollPane = new JScrollPane(checkboxPanel);
        scrollPane.setPreferredSize(new Dimension(200, 100));
        add(scrollPane, BorderLayout.CENTER);
    }

    private void creatStartEndDate() {
        SpinnerDateModel startDateModel = new SpinnerDateModel();
        SpinnerDateModel endDateModel = new SpinnerDateModel();
        startDateSpinner = new JSpinner(startDateModel);
        endDateSpinner = new JSpinner(endDateModel);
        JSpinner.DateEditor startDateEditor = new JSpinner.DateEditor(startDateSpinner, "yyyy-MM-dd");
        startDateSpinner.setEditor(startDateEditor);
        JSpinner.DateEditor endDateEditor = new JSpinner.DateEditor(endDateSpinner, "yyyy-MM-dd");
        endDateSpinner.setEditor(endDateEditor);

        // 获取编辑框并设置字体大小
        initDateFont(startDateSpinner);
        initDateFont(endDateSpinner);

        // 添加到面板
        JPanel spinnerPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 10));
        spinnerPanel.add(new JLabel("开始日期:"));
        spinnerPanel.add(startDateSpinner);
        spinnerPanel.add(new JLabel("结束日期:"));
        spinnerPanel.add(endDateSpinner);
        add(spinnerPanel, BorderLayout.NORTH);
    }

    private void initDateFont(JSpinner dateSpinner) {
        JFormattedTextField editor = ((JSpinner.DefaultEditor) dateSpinner.getEditor()).getTextField();
        editor.setFont(editor.getFont().deriveFont(16f));
    }
}